package mysql;

import java.util.List;

/**
 *
 */
public class SqlGenerator {
    //与备份数据库相关
    /**
     * 生成创建数据库表的语句
     */
    public static String genCreateTableSql(String tableName) {
        return "show create table " + "`" + tableName + "`";
    }

    /**
     * 生成获取列名称的sql
     */
    public static String genColumnNameSql(String tableName) {
        return "show columns from "+ "`" + tableName + "`";
    }

    /**
     * 生成表的insert语句
     */
    public static String genInsertSql(List<String> columns, String table) {
        String columnString = "";
        String columnStringValue = "";
        for(String str : columns) {
            columnString = columnString + genColumn(str);
            columnStringValue = columnStringValue + genColumnValue(str);
        }
        columnString = columnString.substring(0,columnString.length()-1);
        columnStringValue = columnStringValue.substring(0, columnStringValue.length()-4);

        return "SELECT CONCAT" +
                "('insert into `" + table + "` (" + columnString+ ")" +
                " values (\\''" + columnStringValue + ");')" +
                " AS funcdata" +
                " FROM " + "`" + table + "`";
    }

    /**
     * 这个是生成insert sql下半部分
     * @param column
     * @return
     */
    private static String genColumnValue(String column) {
        return ",`" + column + "`,'\\',\\''";
    }

    /**
     * 这个是生成insert sql上部分
     * @param column
     * @return
     */
    private static String genColumn(String column) {
        return "`" + column + "`,";
    }
}
